<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>encryptedstorage_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_storage_encryptedstorage_test.js.html">encryptedstorage_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.storage.EncryptedStorageTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.storage.EncryptedStorageTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.json&#39;);
<a name="line19"></a>goog.require(&#39;goog.storage.EncryptedStorage&#39;);
<a name="line20"></a>goog.require(&#39;goog.storage.ErrorCode&#39;);
<a name="line21"></a>goog.require(&#39;goog.storage.RichStorage&#39;);
<a name="line22"></a>goog.require(&#39;goog.storage.collectableStorageTester&#39;);
<a name="line23"></a>goog.require(&#39;goog.storage.storage_test&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.PseudoRandom&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.storage.FakeMechanism&#39;);
<a name="line28"></a>
<a name="line29"></a>function getEncryptedWrapper(storage, key) {
<a name="line30"></a>  return goog.json.parse(
<a name="line31"></a>      storage.mechanism.get(storage.hashKeyWithSecret_(key)));
<a name="line32"></a>}
<a name="line33"></a>
<a name="line34"></a>function getEncryptedData(storage, key) {
<a name="line35"></a>  return getEncryptedWrapper(storage, key)[goog.storage.RichStorage.DATA_KEY];
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function decryptWrapper(storage, key, wrapper) {
<a name="line39"></a>  return goog.json.parse(
<a name="line40"></a>      storage.decryptValue_(wrapper[goog.storage.EncryptedStorage.SALT_KEY],
<a name="line41"></a>          key, wrapper[goog.storage.RichStorage.DATA_KEY]));
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function hammingDistance(a, b) {
<a name="line45"></a>  if (a.length != b.length) {
<a name="line46"></a>    throw Error(&#39;Lengths must be the same for Hamming distance&#39;);
<a name="line47"></a>  }
<a name="line48"></a>  var distance = 0;
<a name="line49"></a>  for (var i = 0; i &lt; a.length; ++i) {
<a name="line50"></a>    if (a.charAt(i) != b.charAt(i)) {
<a name="line51"></a>      ++distance;
<a name="line52"></a>    }
<a name="line53"></a>  }
<a name="line54"></a>  return distance;
<a name="line55"></a>}
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>function testBasicOperations() {
<a name="line59"></a>  var mechanism = new goog.testing.storage.FakeMechanism();
<a name="line60"></a>  var storage = new goog.storage.EncryptedStorage(mechanism, &#39;secret&#39;);
<a name="line61"></a>  goog.storage.storage_test.runBasicTests(storage);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>function testExpiredKeyCollection() {
<a name="line66"></a>  var mechanism = new goog.testing.storage.FakeMechanism();
<a name="line67"></a>  var clock = new goog.testing.MockClock(true);
<a name="line68"></a>  var storage = new goog.storage.EncryptedStorage(mechanism, &#39;secret&#39;);
<a name="line69"></a>
<a name="line70"></a>  goog.storage.collectableStorageTester.runBasicTests(mechanism, clock,
<a name="line71"></a>      storage);
<a name="line72"></a>}
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>function testEncryption() {
<a name="line76"></a>  var mechanism = new goog.testing.storage.FakeMechanism();
<a name="line77"></a>  var clock = new goog.testing.MockClock(true);
<a name="line78"></a>  var storage = new goog.storage.EncryptedStorage(mechanism, &#39;secret&#39;);
<a name="line79"></a>  var mallory = new goog.storage.EncryptedStorage(mechanism, &#39;guess&#39;);
<a name="line80"></a>
<a name="line81"></a>  // Simple Objects.
<a name="line82"></a>  storage.set(&#39;first&#39;, &#39;Hello world!&#39;);
<a name="line83"></a>  storage.set(&#39;second&#39;, [&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], 1000);
<a name="line84"></a>  storage.set(&#39;third&#39;, {&#39;a&#39;: 97, &#39;b&#39;: 98});
<a name="line85"></a>
<a name="line86"></a>  // Wrong secret can&#39;t find keys.
<a name="line87"></a>  assertNull(mechanism.get(&#39;first&#39;));
<a name="line88"></a>  assertNull(mechanism.get(&#39;second&#39;));
<a name="line89"></a>  assertNull(mechanism.get(&#39;third&#39;));
<a name="line90"></a>  assertUndefined(mallory.get(&#39;first&#39;));
<a name="line91"></a>  assertUndefined(mallory.get(&#39;second&#39;));
<a name="line92"></a>  assertUndefined(mallory.get(&#39;third&#39;));
<a name="line93"></a>
<a name="line94"></a>  // Wrong secret can&#39;t overwrite keys.
<a name="line95"></a>  mallory.set(&#39;first&#39;, &#39;Ho ho ho!&#39;);
<a name="line96"></a>  assertObjectEquals(&#39;Ho ho ho!&#39;, mallory.get(&#39;first&#39;));
<a name="line97"></a>  assertObjectEquals(&#39;Hello world!&#39;, storage.get(&#39;first&#39;));
<a name="line98"></a>  mallory.remove(&#39;first&#39;);
<a name="line99"></a>
<a name="line100"></a>  // Correct key decrypts properly.
<a name="line101"></a>  assertObjectEquals(&#39;Hello world!&#39;, storage.get(&#39;first&#39;));
<a name="line102"></a>  assertObjectEquals([&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], storage.get(&#39;second&#39;));
<a name="line103"></a>  assertObjectEquals({&#39;a&#39;: 97, &#39;b&#39;: 98}, storage.get(&#39;third&#39;));
<a name="line104"></a>
<a name="line105"></a>  // Wrong secret can&#39;t decode values even if the key is revealed.
<a name="line106"></a>  var encryptedWrapper = getEncryptedWrapper(storage, &#39;first&#39;);
<a name="line107"></a>  assertObjectEquals(&#39;Hello world!&#39;,
<a name="line108"></a>      decryptWrapper(storage, &#39;first&#39;, encryptedWrapper));
<a name="line109"></a>  assertThrows(function() {
<a name="line110"></a>    decryptWrapper(mallory, &#39;first&#39;, encryptedWrapper);
<a name="line111"></a>  });
<a name="line112"></a>
<a name="line113"></a>  // If the value is overwritten, it can&#39;t be decrypted.
<a name="line114"></a>  encryptedWrapper[goog.storage.RichStorage.DATA_KEY] = &#39;kaboom&#39;;
<a name="line115"></a>  mechanism.set(storage.hashKeyWithSecret_(&#39;first&#39;),
<a name="line116"></a>                goog.json.serialize(encryptedWrapper));
<a name="line117"></a>  assertEquals(goog.storage.ErrorCode.DECRYPTION_ERROR,
<a name="line118"></a>               assertThrows(function() {storage.get(&#39;first&#39;)}));
<a name="line119"></a>
<a name="line120"></a>  // Test garbage collection.
<a name="line121"></a>  storage.collect();
<a name="line122"></a>  assertNotNull(getEncryptedWrapper(storage, &#39;first&#39;));
<a name="line123"></a>  assertObjectEquals([&#39;one&#39;, &#39;two&#39;, &#39;three&#39;], storage.get(&#39;second&#39;));
<a name="line124"></a>  assertObjectEquals({&#39;a&#39;: 97, &#39;b&#39;: 98}, storage.get(&#39;third&#39;));
<a name="line125"></a>  clock.tick(2000);
<a name="line126"></a>  storage.collect();
<a name="line127"></a>  assertNotNull(getEncryptedWrapper(storage, &#39;first&#39;));
<a name="line128"></a>  assertUndefined(storage.get(&#39;second&#39;));
<a name="line129"></a>  assertObjectEquals({&#39;a&#39;: 97, &#39;b&#39;: 98}, storage.get(&#39;third&#39;));
<a name="line130"></a>  mechanism.set(storage.hashKeyWithSecret_(&#39;first&#39;), &#39;&quot;kaboom&quot;&#39;);
<a name="line131"></a>  storage.collect();
<a name="line132"></a>  assertNotNull(getEncryptedWrapper(storage, &#39;first&#39;));
<a name="line133"></a>  assertObjectEquals({&#39;a&#39;: 97, &#39;b&#39;: 98}, storage.get(&#39;third&#39;));
<a name="line134"></a>  storage.collect(true);
<a name="line135"></a>  assertUndefined(storage.get(&#39;first&#39;));
<a name="line136"></a>  assertObjectEquals({&#39;a&#39;: 97, &#39;b&#39;: 98}, storage.get(&#39;third&#39;));
<a name="line137"></a>
<a name="line138"></a>  // Clean up.
<a name="line139"></a>  storage.remove(&#39;third&#39;);
<a name="line140"></a>  assertUndefined(storage.get(&#39;third&#39;));
<a name="line141"></a>  clock.uninstall();
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>function testSalting() {
<a name="line145"></a>  var mechanism = new goog.testing.storage.FakeMechanism();
<a name="line146"></a>  var randomMock = new goog.testing.PseudoRandom(0, true);
<a name="line147"></a>  var storage = new goog.storage.EncryptedStorage(mechanism, &#39;secret&#39;);
<a name="line148"></a>
<a name="line149"></a>  // Same value under two different keys should appear very different,
<a name="line150"></a>  // even with the same salt.
<a name="line151"></a>  storage.set(&#39;one&#39;, &#39;Hello world!&#39;);
<a name="line152"></a>  randomMock.seed(0); // Reset the generator so we get the same salt.
<a name="line153"></a>  storage.set(&#39;two&#39;, &#39;Hello world!&#39;);
<a name="line154"></a>  var golden = getEncryptedData(storage, &#39;one&#39;);
<a name="line155"></a>  assertRoughlyEquals(&#39;Ciphertext did not change with keys&#39;, golden.length,
<a name="line156"></a>      hammingDistance(golden, getEncryptedData(storage, &#39;two&#39;)), 2);
<a name="line157"></a>
<a name="line158"></a>  // Same key-value pair written second time should appear very different.
<a name="line159"></a>  storage.set(&#39;one&#39;, &#39;Hello world!&#39;);
<a name="line160"></a>  assertRoughlyEquals(&#39;Salting seems to have failed&#39;, golden.length,
<a name="line161"></a>      hammingDistance(golden, getEncryptedData(storage, &#39;one&#39;)), 2);
<a name="line162"></a>
<a name="line163"></a>  // Clean up.
<a name="line164"></a>  storage.remove(&#39;1&#39;);
<a name="line165"></a>  storage.remove(&#39;2&#39;);
<a name="line166"></a>  randomMock.uninstall();
<a name="line167"></a>}
</pre>


</body>
</html>
